<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    var List= function () {
        this.data=[];
        this.size=this.data.length;
        this.pos=0;

    }
    List.prototype={
        constructor:List,
        find: function (elm) {
            //debugger;
            for(var i=0;i<this.size;i++){
                if(this.data[i]==elm)return i;
            }
            return -1;
        },
        contains: function (elm) {
            return !!~this.find(elm);
        },
        length: function () {
            return this.size;
        },
        clear: function () {
            delete this.data;
            this.data=[];
            this.size=this.pos=0;
        },
        toString: function () {
            return this.data;
        },
        getElement: function () {
            return this.data[this.pos];
        },
        insert: function (elm,after) {//在after之后插入
            var idx=this.find(after);
            if(~idx){
                this.data.splice(idx+1,0,elm);
                ++this.size;
                return true;
            }
            return false;
        },
        append: function (elm) {//push
            this.data.push(elm);
            ++this.size;
        },
        remove: function (elm) {
            var idx=this.find(elm);
            console.log('remove idx:',idx);
            if(~idx){
                this.data.splice(idx,1);
                --this.size;
                return true;
            }
            return false;
        },
        frontPos: function () {
            this.pos=0;
        },
        endPos: function () {
            this.pos=this.size-1;
        },
        prevPos: function () {
            if(this.pos>0)--this.pos;
        },
        nextPos: function () {
            if(this.pos<this.size-1)++this.pos;
        },
        currPos: function () {
            return this.pos;
        },
        moveTo: function (idx) {
            if(idx>=0&&idx<this.size)this.pos=idx;
        },
        each: function (cb) {
            for(var i=0;i<this.length();i++){
                cb(this.data[i],i,this.data);
            }
        }
    };


    var list=new List();
    list.append('Cla');
    list.append('Ray');
    list.append('Cyn');
    list.append('Jen');
    list.append('Bry');
    list.append('Dan');
    console.log(list);

    list.frontPos();
    console.log(list.getElement());

    list.nextPos();
    console.log(list.getElement());

    list.nextPos();
    list.nextPos();
    list.prevPos();
    console.log(list.getElement());

    list.each(function (val,i) {
        console.log(i,val);
    });

</script>
</body>
</html>